/*
 * Created by JFormDesigner on Tue Jun 25 08:25:51 CST 2024
 */

package scct.zhangjing.ui;

import scct.zhangjing.dao.UserDao;
import scct.zhangjing.model.User;
import scct.zhangjing.dao.UserDao;
import scct.zhangjing.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author MG
 */
public class FormLogin extends JFrame {
    public static void main(String [] args){
        FormLogin formLogin = new FormLogin();
        formLogin.setVisible(true);
    }

    public FormLogin()
    {
        initComponents();
    }

    private void bthRegister(ActionEvent e) {
        System.out.println("按钮点击了");
    }

    private void btnLogin(ActionEvent e) {
//        System.out.println("按钮点击了");
        String name = txtName.getText();
        String pass= txtPass.getText();
        System.out.println(name+","+pass);
//        name.matches("正则表达式") 返回true表示成功,返回false表达验证失败
       if (!name.matches("\\w{3,10}")){
          JOptionPane.showMessageDialog(null,"你输入的用户名不合法,请检查后重新输入");
        return;

       }
       if(!pass.matches("\\w{3,10}")){
           JOptionPane.showMessageDialog(null,"你输入的密码不合法,请检查后输入");
        return;
       }
        UserDao userDao=new UserDao();
        User user=userDao.login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误,请检查后登录");

        }else{
            JOptionPane.showMessageDialog(this,"登陆成功");
            FormMain formMain =new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
            formMain.setVisible(true);


        }

    }

    private void btnRegister(ActionEvent e) {
//        System.out.println("正在注册");


    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhangjing
        label1 = new JLabel();
        txtName = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        txtPass = new JTextField();
        btnLogin = new JButton();
        btnReg = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u767b\u5f55");
        contentPane.add(label1);
        label1.setBounds(185, 35, 40, label1.getPreferredSize().height);
        contentPane.add(txtName);
        txtName.setBounds(155, 75, 120, 20);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        contentPane.add(label2);
        label2.setBounds(105, 75, 50, label2.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u5bc6 \u7801");
        contentPane.add(label3);
        label3.setBounds(110, 110, 65, 32);
        contentPane.add(txtPass);
        txtPass.setBounds(155, 120, 120, 20);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(110, 180), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(210, 180), btnReg.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhangjing
    private JLabel label1;
    private JTextField txtName;
    private JLabel label2;
    private JLabel label3;
    private JTextField txtPass;
    private JButton btnLogin;
    private JButton btnReg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
